﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Application.System;
using WMS.FC.Application.Technology.Interface;
using WMS.FC.Domain.DataTransferObject.System;
using WMS.FC.Domain.DataTransferObject.Technology;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.DomainObject.Technology;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Technology.Interface;
using WMS.FC.Domain.ValueObject.Technology;

namespace WMS.FC.Application.Technology
{
    public class TechnologyService : ServiceBase, ITechnologyService
    {
        protected readonly IAddTechnologyRouteProcessService addTechnologyRouteProcessService;
        protected readonly IAddTechnologyRouteService addTechnologyRouteService;
        protected readonly IDeleteTechnologyRouteProcessService deleteTechnologyRouteProcessService;
        protected readonly IDeleteTechnologyRouteService deleteTechnologyRouteService;
        protected readonly IEditTechnologyRouteProcessService editTechnologyRouteProcessService;
        protected readonly IEditTechnologyRouteService editTechnologyRouteService;
        protected readonly IGetTechnologyReouteListService getTechnologyReouteListService;
        protected readonly IGetTechnologyRouteProcessListService getTechnologyRouteProcessListService;

        public TechnologyService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            IUnitOfWork unitOfWork,
            IAddTechnologyRouteProcessService addTechnologyRouteProcessService,
            IAddTechnologyRouteService addTechnologyRouteService,
            IDeleteTechnologyRouteProcessService deleteTechnologyRouteProcessService,
            IDeleteTechnologyRouteService deleteTechnologyRouteService,
            IEditTechnologyRouteProcessService editTechnologyRouteProcessService,
            IEditTechnologyRouteService editTechnologyRouteService,
            IGetTechnologyReouteListService getTechnologyReouteListService,
            IGetTechnologyRouteProcessListService getTechnologyRouteProcessListService)
            : base(logger, options, mapper, unitOfWork)
        {
            this.addTechnologyRouteProcessService = addTechnologyRouteProcessService;
            this.addTechnologyRouteService = addTechnologyRouteService;
            this.deleteTechnologyRouteProcessService = deleteTechnologyRouteProcessService;
            this.deleteTechnologyRouteService = deleteTechnologyRouteService;
            this.editTechnologyRouteProcessService = editTechnologyRouteProcessService;
            this.editTechnologyRouteService = editTechnologyRouteService;
            this.getTechnologyReouteListService = getTechnologyReouteListService;
            this.getTechnologyRouteProcessListService = getTechnologyRouteProcessListService;
        }

        public async Task<ResponseBase> GetTechnologyRouteList(RequestBase dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await getTechnologyReouteListService.GetTechnologyReouteList(dTO.UserInfo);
                });

                response = response.TransferResult(result, mapper.Map<List<TechnologyRouteVO>>);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> GetTechnologyRouteProcessList(GetTechnologyRouteProcessListDTO dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await getTechnologyRouteProcessListService.GetTechnologyRouteProcessList(dTO.TechnologyRouteCode, dTO.UserInfo);
                });

                response = response.TransferResult(result, mapper.Map<List<TechnologyRouteProcessVO>>);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> AddTechnologyRoute(AddDataDTO<EditTechnologyRouteDTO> dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await addTechnologyRouteService.AddTechnologyRoute(mapper.Map<TechnologyRouteDO>(dTO.Value), dTO.UserInfo);
                });

                response = response.TransferResult(result);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> AddTechnologyRouteProcess(AddDataDTO<EditTechnologyRouteProcessDTO> dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await addTechnologyRouteProcessService.AddTechnologyRouteProcess(mapper.Map<TechnologyRouteProcessDO>(dTO.Value), dTO.UserInfo);
                });

                response = response.TransferResult(result);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> EditTechnologyRoute(EditDataDTO<EditTechnologyRouteDTO> dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await editTechnologyRouteService.EditTechnologyRoute(mapper.Map<TechnologyRouteDO>(dTO.Value), dTO.UserInfo);
                });

                response = response.TransferResult(result);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> EditTechnologyRouteProcess(EditDataDTO<EditTechnologyRouteProcessDTO> dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await editTechnologyRouteProcessService.EditTechnologyRouteProcess(mapper.Map<TechnologyRouteProcessDO>(dTO.Value), dTO.UserInfo);
                });

                response = response.TransferResult(result);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> DeleteTechnologyRoute(HandleButtonDTO dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await deleteTechnologyRouteService.DeleteTechnologyRoute(dTO.SelectedIds, dTO.UserInfo);
                });

                response = response.TransferResult(result);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }

        public async Task<ResponseBase> DeleteTechnologyRouteProcess(HandleButtonDTO dTO)
        {
            var response = new ResponseBase();
            try
            {
                var result = await unitOfWork.UseTranAsync(async () =>
                {
                    return await deleteTechnologyRouteProcessService.DeleteTechnologyRouteProcess(dTO.SelectedIds, dTO.UserInfo);
                });

                response = response.TransferResult(result);
            }
            catch (Exception ex)
            {
                return response.Exception(ex);
            }

            return response;
        }
    }
}
